package com.guomao.controller;

import com.guomao.entity.User;
import com.guomao.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
@Tag(name = "用户管理", description = "用户的增删改查操作")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping
    @Operation(summary = "获取所有用户", description = "返回系统中所有用户的列表")
    public ResponseEntity<List<User>> getAllUsers() {
        return ResponseEntity.ok(userService.getAllUsers());
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取用户", description = "根据用户ID返回对应的用户信息")
    @ApiResponse(responseCode = "200", description = "用户存在")
    @ApiResponse(responseCode = "404", description = "用户不存在")
    public ResponseEntity<User> getUserById(@Parameter(description = "用户ID", required = true, example = "1") @PathVariable Long id) {
        User user = userService.getUserById(id);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping
    @Operation(summary = "创建用户", description = "创建一个新用户")
    @ApiResponse(responseCode = "200", description = "用户创建成功")
    public ResponseEntity<User> createUser(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "用户信息", required = true)
            @RequestBody User user) {
        return ResponseEntity.ok(userService.createUser(user));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新用户", description = "更新指定ID的用户信息")
    @ApiResponse(responseCode = "200", description = "用户更新成功")
    @ApiResponse(responseCode = "404", description = "用户不存在")
    public ResponseEntity<User> updateUser(
            @Parameter(description = "用户ID", required = true) @PathVariable Long id,
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "更新后的用户信息", required = true) @RequestBody User user) {
        User updatedUser = userService.updateUser(id, user);
        if (updatedUser != null) {
            return ResponseEntity.ok(updatedUser);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "删除指定ID的用户")
    @ApiResponse(responseCode = "204", description = "用户删除成功")
    @ApiResponse(responseCode = "404", description = "用户不存在")
    public ResponseEntity<Void> deleteUser(@Parameter(description = "用户ID", required = true) @PathVariable Long id) {
        if (userService.deleteUser(id)) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}